﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Net.Fan.SmallPig.DataAccess;
using Net.Fan.SmallPig.Common.DBMeta;
using Net.Fan.SmallPig.BusinessRules;
using System.Drawing;

public partial class Workflow_Job : System.Web.UI.Page
{
    long jobId;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack == false)
        {

            string js = Request.Params["J"];
            js = js.Trim();
            long.TryParse(js, out jobId);
            LabelJobId.Text = js;

            HyperLinkModify.NavigateUrl = string.Format("JobModify.aspx?J={0}", js);

            DataRow job = JobDA.GetJob(jobId);

            if (job==null)
            {
                Response.Write("错误,此编号似乎不存在,请检查。");
                Response.End();
            }

            LabelSummary.Text = job[JobMeta.SummaryField] as string;
            LabelCount.Text = job[JobMeta.CountField].ToString();
            LabelUnit.Text = job[JobMeta.UnitField] as string;
            LabelDesc.Text = job[JobMeta.DescriptionField] as string;
            long workflowId = (long)job[JobMeta.WorkflowIDField];
            LabelWorkflowId.Text = workflowId.ToString();
            LabelState.Text = job[JobMeta.CurrentStateField] as string; ;
            LabelWorkflowName.Text = WorkflowDA.GetWorkflowName(workflowId);
            LabelPlanFinishTime.Text = job[JobMeta.PlanFinishTimeField].ToString();
            LabelCustomer.Text = job[JobMeta.CustomerField] as string;

            if (JobBiz.IsOverdue(jobId))
            {
                LabelPlanFinishTime.BackColor = Color.Red;
                LabelPlanFinishTime.ForeColor = Color.White;
                LabelPlanFinishTime.Text += " [已逾期]";
            }

            //CreateOperateButtons(workflowId);

            //get operation history
            DataTable history = JobDA.GetOperationHistoy(jobId);
            datalistLog.DataSource = history;
            datalistLog.DataBind();

            ImageMap1.ImageUrl = string.Format("CreateJobGraph.aspx?J={0}", jobId);
            OperatePicture dd = OperatePicture.CreateInstance(jobId.ToString());

            RectangleHotSpot[] rhs = dd.GetHotSpot();
            foreach (RectangleHotSpot r in rhs)
            {
                ImageMap1.HotSpots.Add(r);
            }

        }
    }

    private void CreateOperateButtons(long workflowId)
    {
        DataTable operates = JobBiz.GetWhosOperates(workflowId, LabelState.Text, Page.User.Identity.Name);

        foreach (DataRow item in operates.Rows)
        {
            HyperLink b = new HyperLink();
            b.Text = item[WorkflowMeta.Operate] as string;
            b.Font.Bold = true;
            b.Font.Size = FontUnit.XLarge;
            //b.CommandName = item[WorkflowMeta.CurrentState] as string;
            //b.CommandArgument = item[WorkflowMeta.NextState] as string;
            //b.Command += new CommandEventHandler(b_Command);
            b.NavigateUrl = string.Format("DoOperate.aspx?J={0}&A={1}", jobId, item[WorkflowMeta.ActivityID]);
            b.CssClass = "ButtonUrl";
            //b.EnableViewState = true;
            Panel1.Controls.Add(b);
        }
    }


}
